package ${packageValue}.service.impl;

import ${packageValue}.dto.${entityName}Dto;
import ${packageValue}.entity.${entityName};
import ${packageValue}.query.${entityName}Query;
import ${packageValue}.service.${entityName}Service;
import ${packageValue}.vo.${entityName}Vo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yupaits.yutool.commons.constant.DateTimeConstants;
import com.yupaits.yutool.commons.service.OptService;
import com.yupaits.yutool.orm.core.support.AuditLogger;
import com.yupaits.yutool.orm.core.support.EntityBuilder;
import com.yupaits.yutool.orm.core.support.VoBuilder;
import com.yupaits.yutool.orm.mybatis.base.MybatisServiceImpl;
import com.yupaits.yutool.orm.mybatis.support.PageQuery;
import com.yupaits.yutool.plugin.importexport.core.ExportTemplate;
import com.yupaits.yutool.plugin.importexport.core.ImportTemplate;
import com.yupaits.yutool.plugin.importexport.support.ExportProps;
import com.yupaits.yutool.plugin.importexport.support.ImportProps;
import com.yupaits.yutool.plugin.importexport.support.ImportResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * ${comment}Service实现
 * @author ${author}
 * @since ${date}
 */
@Service("${entityObj}Service")
public class ${entityName}ServiceImpl extends MybatisServiceImpl<Long, ${entityName}> implements ${entityName}Service {
    private final ImportTemplate importTemplate;
    private final ExportTemplate exportTemplate;

    @Autowired
    public ${entityName}ServiceImpl(OptService optService, AuditLogger auditLogger, ImportTemplate importTemplate, ExportTemplate exportTemplate) {
        super(${entityName}.class, optService, auditLogger);
        this.importTemplate = importTemplate;
        this.exportTemplate = exportTemplate;
    }

    @Override
    public void setDefaultVoConfig() {
        setDefaultVoClass(${entityName}Vo.class);
        setDefaultVoBuilder((VoBuilder<${entityName}Vo, ${entityName}>) (vo, model) -> {
            
        });
    }

    @Override
    public void setDefaultEntityBuilder() {
        setDefaultEntityBuilder((EntityBuilder<${entityName}, ${entityName}Dto>) (model, dto) -> {
            
        });
    }

    @Override
    public IPage<${entityName}Vo> page(PageQuery<${entityName}Query> pageQuery) {
        return pageVo(pageQuery);
    }

    @Override
    public List<${entityName}Vo> list(${entityName}Query query) {
        return listVo(query.buildNewLambdaQuery());
    }

    @Override
    public ${entityName}Vo details(Long id) {
        return getVoById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void save(${entityName}Dto dto) {
        saveDto(dto);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteById(Long id) {
        removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ImportResult importData(MultipartFile file) {
        return importTemplate.importData(file, ImportProps.<Long, ${entityName}, ${entityName}Dto>builder().clazz(${entityName}Dto.class).dataService(this).build());
    }

    @Override
    public void exportData(HttpServletResponse response, ${entityName}Query query) throws IOException {
        exportTemplate.export(listVo(query.buildNewLambdaQuery()), ExportProps.<${entityName}Vo>builder().clazz(${entityName}Vo.class)
            .filename(String.format("${comment}_%s.xlsx", LocalDate.now(DateTimeConstants.ZONE_ID).toString())).build(), response);
    }
}